package com.example.presentshopping.ui.fragment

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.CategoryListBean
import com.example.presentshopping.config.bean.IamgeViewBean
import com.example.presentshopping.config.bean.SpellgroupBean
import com.example.presentshopping.config.bean.YDClassifyItemBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.activity.ProductListActivity
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import com.example.presentshopping.utils.tool.JsonHelper

class YDClassifyItemFragment(var categoryListBean: CategoryListBean.DataBean.ListBean) :
    BaseMvpFragment() {

    var name_tv: TextView? = null
    var All_tv: TextView? = null
    var yd_rc: RecyclerView? = null
    var ydClassifyItemBean: YDClassifyItemBean? = null
    private var queryInfoPresenter: PublicPresenter? = null


    var baseRecyclerAdapter: BaseRecyclerAdapter<YDClassifyItemBean.DataBean.ListBean>? = null
    override fun initView(view: View?, savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "")
        queryInfoPresenter?.attachView(this)
        name_tv = view?.findViewById(R.id.name_tv)
        All_tv = view?.findViewById(R.id.All_tv)
        yd_rc = view?.findViewById(R.id.yd_rc)

        setAdapter()
        Presenter()

    }


    fun setAdapter() {
        yd_rc?.layoutManager = GridLayoutManager(context, 3)
        baseRecyclerAdapter = object : BaseRecyclerAdapter<YDClassifyItemBean.DataBean.ListBean>(
            context,
            null,
            R.layout.item_ydlassifytem
        ) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: YDClassifyItemBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val tun_iv = holder?.getView<ImageView>(R.id.tun_iv)
                val  name_inse =holder?.getView<TextView>(R.id.name_inse)

                name_inse?.text = data?.name
                GlideUtil.load(context, data.thumb, tun_iv)
                tun_iv?.setOnClickListener {
                    val jsons = JsonHelper.newtoJson(ydClassifyItemBean)
                    val initda = Intent(context, ProductListActivity().javaClass)
                    initda.putExtra("categoryListBean",categoryListBean.name)
                    initda.putExtra("categoryid",categoryListBean.id)
                    initda.putExtra("data",jsons)
                    initda.putExtra("position",position.toString())
                    context?.startActivity(initda)
                }
            }

        }

        yd_rc?.adapter = baseRecyclerAdapter
    }

    override fun onDestroyView() {
        queryInfoPresenter?.detachView()
        super.onDestroyView()
    }

    fun Presenter() {
        queryInfoPresenter?.netWorkRequestPost(
            HttpURL.Req_getGoodsCategoryChild,
            HttpRequest.getGoodsCategory(categoryListBean.id.toString())
        )
    }

    override fun onSuccess(data: String?) {
        ydClassifyItemBean = GsonUtils.getGson().fromJson(data, YDClassifyItemBean().javaClass)
        baseRecyclerAdapter?.setData(ydClassifyItemBean?.data?.list)

    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override val layoutId: Int
        get() = R.layout.ylassifytem_gragment
}